<template>
  <div class='app-container'>
     <div class="app-box">
      <div class="day-top clearfix">
         <div class="top">
          <span>出单时间：</span>
           <el-date-picker
            class='wd230'
            size="mini"
            value-format='yyyy-MM-dd'
            v-model="time1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
          <span class="islabel">组织机构：</span>
          <ztree-select class="inputbox" v-model="organzationId" :treedata="treeData" :childprops="childprops"></ztree-select>
          <span class="islabel">上/下游政策：</span>
          <el-select v-model="Ptype" class='inputbox' size='mini'>
            <el-option v-for="item in isActive" :key="item.id" :label="item.content
                  " :value="item.id">
            </el-option>
          </el-select>
        </div>
        <el-button size="mini" type="primary" style="float:right" v-on:click='search()' class='unit-sbtn'>搜索</el-button>
      </div>

      <div class="polocy-table">
          <el-table
            :data="tableData"
            v-loading="loadinging"
            stripe
            style="width: 100%">
            <el-table-column
            prop="insuranceName"
            label="保险公司"
            
           >
            </el-table-column>
            <el-table-column
            prop="guaranteeCount"
            align="right"
            label="保单总量"
           >
            </el-table-column>
            <el-table-column
            	 align="right"
            prop="rightbaodan"
            label="已匹配政策保单量">
            <template slot-scope="scope">
                <span class='rightbaodan' @click="toPolicyformdetail(scope.row.id)">{{scope.row.haveMatchPolicyCount}}</span>
            </template>
            </el-table-column>
               <el-table-column
               	 align="right"
            prop="notMatchPolicyCount"
            label="未匹配政策保单量">
            </el-table-column>
             <el-table-column
	             align="right"
	            label="匹配率">
	            <template slot-scope="scope">
                <span>{{scope.row.profit}} %</span>
            </template>
	           </el-table-column>
	           <el-table-column
	            width="30"
	           >
	          
	           </el-table-column>
        </el-table>
      </div>
     </div>
  </div>
</template>
<script>
import ztreeSelect from "@/components/z-select/index.vue";
export default {
  components: {
    ztreeSelect
  },
  data() {
    return {
        time1:'',
        tableData: [],
        loadinging:false,
        treeData:[],
        organzationId:'',
        Ptype:'1',
        //数组子级相关参数
        childprops: {
          name: "displayName", //显示名字
          children: "orgs", //子级字段名
          lv: "depth" //当前层级
        },
        isActive: [
        {
          id: "1",
          content: "上游"
        },
        {
          id: "2",
          content: "下游"
        }
      ],
    };
  },
  mounted() {
    if (this.$route.query.startDay) {
    this.time1 = [this.$route.query.startDay, this.$route.query.endDay];
    }else{
//    let now = new Date().getFullYear();
//    let a = new Date().getFullYear() + "-01-01";
//    let last = new Date(now,12,0);
//    let b = this.formatDate(last).substring(0,10);
//    this.time1 = [a, b];
			let years=new Date().getFullYear()
			let months=new Date().getMonth()+1<10?'0'+(new Date().getMonth()+1):new Date().getMonth()+1;
			let last=new Date(years,months,0).getDate()
			let startday=years+"-"+months+"-01"
			let lastday=years+"-"+months+"-"+last
			this.time1 = [startday, lastday];
			console.log(this.time1)
    }
    this.getList();
    this.getUnits();
  },
  computed: {
   
  },
  created() {
    
  },
  watch:{
    $route(to, from) {
        if (this.$route.query.startDay) {
        this.time1 = [this.$route.query.startDay, this.$route.query.endDay];
      }else{
        let now = new Date().getFullYear();
        let a = new Date().getFullYear() + "-01-01";
        let last = new Date(now,12,0);
        let b = this.formatDate(last).substring(0,10);
        this.time1 = [a, b];
      }
    }   
  },
  methods: {
    getList(){
    	this.loadinging=true
      let params = {
        beginDate:this.time1[0],
        endDate:this.time1[1],
        // organzationId:this.organzationId,
        // Ptype:this.Ptype
      }
      this.post(params,'services/app/report/PolicyMatch').then(rs => {
        if(rs.success){
          this.tableData = rs.result.items;
          this.loadinging=false
        }else{
           this.loadinging=false;
           this.$notify.error({
							title: '错误',
							message: rs.error.message
						});
        }
      })
    },
    // 格式化
    formatDate(time) {
      var date = new Date(time);

      var year = date.getFullYear(),
        month = date.getMonth() + 1, //月份是从0开始的
        day = date.getDate(),
        hour = date.getHours(),
        min = date.getMinutes(),
        sec = date.getSeconds();
      var newTime =
        year +
        "-" +
        (month < 10 ? "0" + month : month) +
        "-" +
        (day < 10 ? "0" + day : day) +
        " " +
        (hour < 10 ? "0" + hour : hour) +
        ":" +
        (min < 10 ? "0" + min : min) +
        ":" +
        (sec < 10 ? "0" + sec : sec);
      return newTime;
    },
   toPolicyformdetail(val){
        this.$router.push({
          name:'policyformdetail',
          query:{
            startDay:this.time1[0],
            endDay :this.time1[1],
            id:val
          }
        })
   },
   search(){
        this.$router.push({
          name:'policyform',
          query:{
            startDay:this.time1[0],
            endDay :this.time1[1],
            time:new Date().getTime()
          }
        })
   },
   getUnits() {
      let params = {
        queryType: 1
      };
      this.post(
        params,
        "services/app/common/GetCurrentOrganizationUnitsByType"
      ).then(rs => {
        console.log("组织机构" + rs);
        if (rs.success) {
          let data = rs.result;
          let arr = [];
          arr.push(data);
          this.treeData = arr;
        }
      });
    },
  }
};
</script>
<style lang="scss" scoped>
.islabel{
  margin-left:10px;
}
.app-box {
  color: #333;
  .day-top {
    margin-bottom: 15px;
    color: #666;
    .top {
      display: inline-block;
      margin-bottom: 15px;
    }
  }
  .polocy-table{
    // margin-top: 30px;
    .rightbaodan{
        color: #027dca;
        cursor: pointer;
    }
  }
}
</style>